package com.simplehire.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.util.HashMap;
import java.util.Map;

@Service
public class DeepSeekService {

    @Value("${deepseek.api.url:https://api.deepseek.com/v1/chat/completions}")
    private String apiUrl;

    @Value("${deepseek.api.key:your_api_key_here}")
    private String apiKey;

    private final RestTemplate restTemplate;

    public DeepSeekService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String sendPrompt(String prompt) {
        try {
            // 创建请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            headers.set("Authorization", "Bearer " + apiKey);

            // 创建请求体
            Map<String, Object> requestBody = new HashMap<>();
            requestBody.put("model", "deepseek-chat");
            requestBody.put("messages", new Object[]{
                    Map.of("role", "user", "content", prompt)
            });
            requestBody.put("temperature", 0.7);
            requestBody.put("max_tokens", 2000);

            // 发送请求
            HttpEntity<Map<String, Object>> entity = new HttpEntity<>(requestBody, headers);
            ResponseEntity<Map> response = restTemplate.exchange(
                    apiUrl,
                    HttpMethod.POST,
                    entity,
                    Map.class
            );

            // 处理响应
            if (response.getStatusCode() == HttpStatus.OK && response.getBody() != null) {
                Map<String, Object> responseBody = response.getBody();

                // 解析响应
                if (responseBody.containsKey("choices")) {
                    Object choices = responseBody.get("choices");
                    if (choices instanceof java.util.List && !((java.util.List<?>) choices).isEmpty()) {
                        Object firstChoice = ((java.util.List<?>) choices).get(0);
                        if (firstChoice instanceof Map) {
                            Object message = ((Map<?, ?>) firstChoice).get("message");
                            if (message instanceof Map) {
                                Object content = ((Map<?, ?>) message).get("content");
                                if (content != null) {
                                    return content.toString();
                                }
                            }
                        }
                    }
                }

                throw new RuntimeException("API响应格式不正确: " + responseBody);
            } else {
                throw new RuntimeException("API请求失败: " + response.getStatusCode());
            }
        } catch (Exception e) {
            throw new RuntimeException("调用DeepSeek API时发生错误: " + e.getMessage(), e);
        }
    }
}